package com.cjst.controller.base;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.lang3.StringUtils;
import org.apache.log4j.Logger;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.cjst.controller.base.BaseController;
import com.cjst.pojo.base.ReportTemplate;
import com.cjst.pojo.sys.SysUser;
import com.cjst.sdp.util.MapUtils;
import com.cjst.service.base.ReportTemplateService;
import com.cjst.sql.C;
import com.cjst.sql.Method;
import com.cjst.sql.WherePrams;
import com.cjst.util.CloneClass;
import com.cjst.util.UserPrincipalUtil;
import com.cjst.util.code.PageUtil;
import com.cjst.util.json.ResultObj;

@Controller
@RequestMapping("/base/reportTemplate")
public class ReportTemplateController extends BaseController{
	private static final Logger log = Logger.getLogger(ReportTemplateController.class);
	@Autowired
	private ReportTemplateService reportTemplateService;
	
	
	@RequestMapping(value = "", method = RequestMethod.GET)
    //@RequiresPermissions("reportTemplate")
    public String toReportTemplate() {
        return "base/reportTemplate/reportTemplate";
    }
	
	/**
     * 跳转到添加页面
     */
    @RequestMapping(value = "/add", method = RequestMethod.GET)
    @RequiresPermissions("report_template_add")
    public String addReportTemplater() {
        return "base/reportTemplate/reportTemplate_add";
    }
	
	
	@RequestMapping(value = "/list")
    @ResponseBody
    public Object list() {
		
		WherePrams wherePrams = Method.where("delFlag", C.EQ, 0);
		
		PageUtil<ReportTemplate> pageUtil = reportTemplateService.listPageCount(wherePrams);
        List<ReportTemplate> reportTemplates = pageUtil.getData();
		
        List<Map<String, Object>> maps = new ArrayList<>();
        for (ReportTemplate reportTemplate : reportTemplates) {
            Map<String, Object> reportTemplateMap = MapUtils.transBean2Map(reportTemplate);
            maps.add(reportTemplateMap);
        }
        
        
        PageUtil<Map<String, Object>> pages = new PageUtil<>();

        CloneClass.getClone(pageUtil, pages);
        pages.setData(maps);
        return this.packForBT(pages);
	}
	
	
	@RequestMapping(value = "/listAll")
	@ResponseBody
	public Object listAll() {
		
		WherePrams wherePrams = Method.where("delFlag", C.EQ, "0");
		
		
		PageUtil<ReportTemplate> pageUtil = reportTemplateService.listPageCount(wherePrams);
        List<ReportTemplate> reportTemplates = pageUtil.getData();
		
        List<Map<String, Object>> maps = new ArrayList<>();
        for (ReportTemplate reportTemplate : reportTemplates) {
            Map<String, Object> reportTemplateMap = MapUtils.transBean2Map(reportTemplate);
            maps.add(reportTemplateMap);
        }
        
        
        
        PageUtil<Map<String, Object>> pages = new PageUtil<>();

        CloneClass.getClone(pageUtil, pages);
        pages.setData(maps);
        return this.packForBT(pages);
	}
	
	
	@RequestMapping(value = "/save/addOne", method = RequestMethod.POST)
    @ResponseBody
    @RequiresPermissions("report_template_add")
    public Object addOne(ReportTemplate reportTemplate, HttpServletRequest req) {
//		if (StringUtils.isEmpty(reportTemplate.getId())) {
//			return new ResultObj(1, "参数传递错误，父类id不能为空 ,失败", null);
//		}
		
		
		
		String flag=reportTemplateService.addLocalByid(reportTemplate);
		
		
		return new ResultObj(0, "保存成功", flag);
	}
	
	 /**
     * 部件类型详情
     * @param reportTemplateId
     * @param model
     * @return
     */
    @RequestMapping(value = "/view/{reportTemplateId}", method = RequestMethod.GET)
    @RequiresPermissions("report_template_view")
    public String viewUser(@PathVariable String reportTemplateId, Model model) {
        WherePrams where = Method.where("id", C.EQ, reportTemplateId);
        ReportTemplate reportTemplate = reportTemplateService.get(where);
        if (reportTemplate != null) {
            Map<String, Object> reportTemplateMap = MapUtils.transBean2Map(reportTemplate);
            model.addAttribute("report_template", reportTemplateMap);
        }
        return "base/reportTemplate/reportTemplate_detail";
    }

	
	@RequestMapping(value = "/save/addMany")
    @ResponseBody
    public Object addMany(ReportTemplate reportTemplate) {
		
		
		return null;
	}
	
	@RequestMapping(value = "/delete/deleteOne")
    @ResponseBody
    public Object deleteOne(ReportTemplate reportTemplate) {
		
		
		return null;
	}
	
	@RequestMapping(value = "/save/delete")
    @ResponseBody
    @RequiresPermissions("report_template_delete")
    public Object saveDelete(String ids) {
		 if (!StringUtils.isNotEmpty(ids)) {
	            return new ResultObj(1, "参数传递错误", null);
	        }
	        String[] list = ids.split(",");
	        for (String id : list) {
	            //ReportTemplate reportTemplate2 = reportTemplateService.get(id);
	            WherePrams wherePrams = Method.where("id", C.EQ, id);
	            ReportTemplate reportTemplate1 = new ReportTemplate();
	            reportTemplate1.setDelFlag(1);
	            reportTemplateService.updateLocal(reportTemplate1, wherePrams);
	            
	            /*
	             * 同时删除RELEVANCE
	             */
	            
	            
	        }

	        return new ResultObj(0, "删除成功", null);
	}
	
	
	@RequestMapping(value = "/edit/{reportTemplateId}", method = RequestMethod.GET)
	@RequiresPermissions("report_template_edit")
	public Object toUpdate(@PathVariable String reportTemplateId, Model model) {
		WherePrams where = Method.where("id", C.EQ, reportTemplateId);
		ReportTemplate reportTemplate = reportTemplateService.get(where);

		if (reportTemplate != null) {
			Map<String, Object> userMap = MapUtils.transBean2Map(reportTemplate);
			model.addAttribute("report_template", userMap);

		}

		return "base/reportTemplate/reportTemplate_edit";
	}
	
	
	@RequestMapping(value = "/save/updateOne", method = RequestMethod.POST)
    @ResponseBody
    public Object updateOne(ReportTemplate reportTemplate) {
		if (reportTemplate == null) {
            return new ResultObj(1, "参数传递错误，修改失败", null);
        }
        WherePrams where = Method.where("id", C.EQ, reportTemplate.getId());
        ReportTemplate reportTemplate2 = reportTemplateService.get(where);
        if (reportTemplate2 == null) {
            return new ResultObj(1, "根据Id未查询到部件信息，修改失败", null);
        }
      
        /*
         * 
         * 往后需添加一系列修改规则
         * 
         */
        

        int flag = reportTemplateService.updateLocal(reportTemplate);
        //updateRedisUser();
        return new ResultObj(0, "修改成功", flag);
	}
	

}
